<?php

namespace backend\controllers;

use common\models\User;
use Yii;
use common\models\LoginForm;

class SiteController extends BaseController
{
    public function behaviors()
    {
        return [
        ];
    }

    public function actions()
    {
        return [
            'error' => [
                'class' => 'yii\web\ErrorAction',
            ],
        ];
    }

    public function actionIndex()
    {
        return $this->render('index');
    }

    public function actionHome()
    {
        $user = Yii::$app->user;
        $this->layout = "main";
        return $this->render('home');
    }

    /**
     * @desc
     * @return string|\yii\web\Response
     * @Date: 2024/10/31
     */
    public function actionLogin()
    {
        $user = Yii::$app->user;
        if (!$user->isGuest) {//是客户 已登录
            return $this->redirect(User::login_to_url());
        }

        $model = new LoginForm();
        if ($model->load(Yii::$app->request->post()) && $model->login()) {
            return $this->redirect(User::login_to_url());
        }

        $model->password = '';
        return $this->render('login', [
            'model' => $model,
        ]);
    }

    public function actionLogout()
    {
        Yii::$app->user->logout();
        return $this->redirect(['/site/login']);
    }
}
